// pages/home/product/preproduct/preproduct.js
const globalIp = getApp().globalData.userIp
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    pageIndex:1,
    pageSize:100,
    total: 0,
    IndexList: [],
    popup: false,
    info: '客户选择',
    infoValue: '',
    screen: '',
    screenCode: '',
    screenFlag: false//控制综合下拉框是否显示
  },

  getInputValue(e) {
    this.setData({
      info: e.detail.value
    })
  },

  selectData: function(){
    this.allShow();
  },

  clickTap(e){
    this.setData({
      popup: true
    })
    let that = this;
    wx.request({
      url: globalIp + 'home/oitm/preProduct',
      data: {
        openId: wx.getStorageSync('userInfo')?wx.getStorageSync('userInfo').openid:'',
        value: 2,
        cardCode: e.currentTarget.dataset.code,
        pageIndex: 0,
        pageSize: 0
      },
      header: {
        'content-type': 'application/json'
      },
      method: 'POST',
      success: function(res) {
        that.setData({
          IndexList: res.data.jsonObject.data
        })
      }
    })
  },

  clickTapClose(){
    this.setData({
      popup: false,
      IndexList: []
    })
  },

  allShow: function(){
    var that = this;
    wx.showLoading({
      title: '数据加载中...',
    })
    wx.request({
      url: globalIp + 'home/oitm/preProduct',
      data: {
        pageSize: this.data.pageSize,
        pageIndex: this.data.pageIndex,
        value: 5,
        openId: wx.getStorageSync('userInfo').openid,
        userName: that.data.infoValue 
      },
      header: {
        'content-type': 'application/json'
      },
      method: 'POST',
      success: function(res) {
        that.setData({
          list: res.data.jsonObject.data
        })
      },
      complete: () => {
        wx.hideLoading()
      }
    })
  },

  selectShow(e) {
    wx.navigateTo({
      url: '../preproductIndex/preproductIndex?CardCode='+e.currentTarget.dataset.code
      +'&type='+e.currentTarget.dataset.type,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      search: this.search.bind(this),
    })
  },

  // 搜索框获取客户信息
  search: function (value) {
    var that = this;
    var cardInfo = [];
    var result = new Object();
    result.text = '';
    result.value = '';
    var resultList=[];
    wx.request({
      url: globalIp + 'home/ocrd/getCardInfo',
      data: {
        value: value,
        openId: that.data.openid || wx.getStorageSync('userInfo').openid,
        login: wx.getStorageSync('userInfo').login || 'N'
      },
      header: {
        'content-type': 'application/json'
      },
      method: 'POST',
      success: function(res) {
        cardInfo = res.data.cardInfo.data
        // console.log(res.data.cardInfo.data)
      },
      fail: function(res) {
        console.log("请求失败")
      }
    })
    return new Promise((resolve, reject) => {
      setTimeout(() => {
          // resolve([{text: '搜索结果', value: 1}, {text: '搜索结果2', value: 2}])
          if (cardInfo) {
            for (var i of cardInfo) {
              result.text = i.CardName;
              result.value = i.CardCode;
              resultList.push(result);
              result = {};
            }
          }
          resolve(resultList);
      }, 200)
    })      
  },
  selectResult: function (e) {
    // console.log('select result', e.detail)
    this.setData({
      screenFlag: false,
      screen: e.detail.item.text,
      screenCode: e.detail.item.value,
      info: e.detail.item.text,
      infoValue: e.detail.item.value
    })
    this.allShow()
  },

  screenShow: function () {//综合下拉
    if (this.data.screenFlag){
      this.setData({
        screenFlag : !this.data.screenFlag
      })
    }else{
      this.setData({
        screenFlag: !this.data.screenFlag
      })
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    // 页面渲染完成
    // this.allShow()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

})